package com.wing.product.controller.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.request.ProcessInfoForm;
import com.wing.product.model.response.ProcessInfoVO;
import com.wing.product.service.ProcessInfoService;
import com.wing.web.db.config.util.DynamicDataSource;
import com.wing.web.utils.LocaleMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-工序列表管理", tags = "管理")
@RestController("appProcessInfoController")
@RequestMapping("product/app/processInfo")
public class ProcessInfoController {

    @Autowired
    private ProcessInfoService processInfoService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "工序列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProcessInfoVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , @RequestParam(value = "productTypeId", required = false) Long productTypeId
            , @RequestParam(value = "type", required = false) Long type
            , @RequestParam(value = "code", required = false) String code
            , @RequestParam(value = "roomInfoId", required = false) Long roomInfoId
            , String query
    ) {
        Page<ProcessInfoVO> pg = new Page<>(page, limit);
        Map map = new HashMap();
        map.put("order", order);
        map.put("productTypeId", productTypeId);
        map.put("type", type);
        if (StringUtils.isNotBlank(query)){
            map.put("query",query);
        }
        //查询已经存在的工序
        IPage<ProcessInfoVO> list = processInfoService.pageList4app(pg, map);
        if (type != null) {
            if (1 == type) {
                Map newMap = new HashMap();
                newMap.put("order", order);
                //查询所有工序
                Page<ProcessInfoVO> pg2 = new Page<>(page, limit);
                IPage<ProcessInfoVO> allList = processInfoService.pageList4app(pg2, newMap);
                for (ProcessInfoVO all : allList.getRecords()) {
                    for (ProcessInfoVO listRecord : list.getRecords()) {
                        if (all.getId().equals(listRecord.getId())) {
                            all.setElect(true);
                        }
                    }
                }
                return new PageResult<>(allList.getRecords(), allList.getTotal());
            }
        }
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long processInfoId) {
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        ProcessInfoVO processInfoVO = processInfoService.get4appById(processInfoId, databaseUser);
        if (processInfoVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), processInfoVO);
    }

    @PostMapping("/add")
    public JsonResult add(@RequestBody ProcessInfoForm processInfoForm) {
        Map map = new HashMap();
        map.put("nodeNames", processInfoForm.getNodeNames());
        List list = processInfoService.list4app(map);
        if (list.size() > 0) {
            return JsonResult.fail("添加失败,工序名称重复");
        }
        if (processInfoService.add(processInfoForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

}
